# Specify the main target
TARGET = ./bin/test
# Default build type
TYPE = $(word 2,$(MAKECMDGOALS))
# Which directories contain source files
# Engine
DIRS = 	src \
		src/engine \
		src/engine/core \
		src/engine/gl \
		src/engine/iniparser \
		src/engine/scene \
		src/engine/system \
		src/engine/tinyxml \
		src/engine/ut
# Project
SOURCE = 	src/tests/test_common.cpp \
			src/tests/RDATest.cpp
HEADERS = 	src/tests/test_common.h \
			src/test/RDATest.h
# Which libraries are linked
LIBS = 
# Dynamic libraries
DLIBS = 
# Linker options
LDPARAM = -s
# Compiler options
CCPARAM = -Wall -O2
# Compiler defines
MACROS = ENGINE_GLFW

# Win32 Build
ifeq ($(TYPE),win32)
LIBS += glfw opengl32 glu32 comdlg32
# Linux Build
else
TYPE = linux
LIBS += glfw GLU GL
endif

# Add directories to the include and library paths
INCPATH = .
LIBPATH = 

# Which files to add to backups, apart from the source code
EXTRA_FILES = Makefile
# The compiler
C++ = g++

# Where to store object and dependancy files.
STORE = .make-$(TYPE)
# Makes a list of the source (.cpp) files.
SOURCE += $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.cpp))
# List of header files.
HEADERS += $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.hpp))
# Makes a list of the object files that will have to be created.
OBJECTS := $(addprefix $(STORE)/, $(SOURCE:.cpp=.o))
# Same for the .d (dependancy) files.
DFILES := $(addprefix $(STORE)/,$(SOURCE:.cpp=.d))

# Specify phony rules. These are rules that are not real files.
.PHONY: all build clean backup dirs helpme

all: helpme

# Main target. The @ in front of a command prevents make from displaying
# it to the standard output.
build:  dirs $(OBJECTS)
		@echo Linking $(TARGET).
		@$(C++) -o $(TARGET) $(OBJECTS) $(LDPARAM) $(foreach LIBRARY, \
				$(LIBS),-l$(LIBRARY)) $(foreach LIB,$(LIBPATH),-L$(LIB))

# Rule for creating object file and .d file, the sed magic is to add
# the object path at the start of the file because the files gcc
# outputs assume it will be in the same dir as the source file.
$(STORE)/%.o: %.cpp
		@echo Creating object file for $*...
		@$(C++) -Wp,-MMD,$(STORE)/$*.dd $(CCPARAM) $(foreach INC,$(INCPATH),-I$(INC))\
				$(foreach MACRO,$(MACROS),-D$(MACRO)) -c $< -o $@
		@sed -e '1s/^\(.*\)$$/$(subst /,\/,$(dir $@))\1/' $(STORE)/$*.dd > $(STORE)/$*.d
		@rm -f $(STORE)/$*.dd

# Empty rule to prevent problems when a header is deleted.
%.hpp: ;

# Cleans up the objects, .d files and executables.
clean:
		@echo Making clean.
		@-rm -f $(foreach DIR,$(DIRS),$(STORE)/$(DIR)/*.d $(STORE)/$(DIR)/*.o)
		@-rm -f -d -r $(STORE)
		@-rm -f $(TARGET)

# Backup the source files.
backup:
		@-if [ ! -e .backup ]; then mkdir .backup; fi;
		@zip .backup/backup_`date +%d-%m-%y_%H.%M`.zip $(SOURCE) $(HEADERS) $(EXTRA_FILES)

# Create necessary directories
dirs:
		@-if [ ! -e $(STORE) ]; then mkdir $(STORE); fi;
		@-$(foreach DIR,$(DIRS), if [ ! -e $(STORE)/$(DIR) ]; \
		then mkdir $(STORE)/$(DIR); fi; )

helpme:
	@echo " "
	@echo " -----------------------------------------------------------------------------"
	@echo " Usage:		make <command> <target>"
	@echo " "
	@echo " <command>:	build, clean, backup, dirs, helpme"
	@echo " <target>:	linux, win32"
	@echo " -----------------------------------------------------------------------------"
	@echo " "
	
win32:
	@echo "Done."
	
linux:
	@echo "Done."
	
	
# Includes the .d files so it knows the exact dependencies for every
# source.
-include $(DFILES)
